package com.weibo.demos.web.Service.impl;

import com.weibo.demos.web.Mapper.UserMapper;
import com.weibo.demos.web.Service.UserService;
import com.weibo.demos.web.entity.UserVO;
import com.weibo.demos.web.entity.userLoginDTO;
import com.weibo.demos.web.entity.userRegDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


@Service
public class UserServiceImpl implements UserService {



    @Autowired
    private UserMapper userMapper;


    @Override
    public int addUser(userRegDTO user) {

        //先查询数据库里面用户名是否存在
        userRegDTO n =userMapper.selectUserName(user);
        System.out.println("注册时查询到的用户信息"+n);
        if (n!=null){
            return 2;
        }
        //不存在，注册新用户
        userMapper.addUser(user);
        return 1;
    }

    @Override
    public UserVO userLogin(userLoginDTO user) {
        //先查询数据库是否有该用户，没有则返回3
        UserVO u =userMapper.selectUserName1(user);


        System.out.println("登录时查询到的用户信息"+u);
        if (u==null){
            return null;
        }

        //如果有该用户，则对比密码是否相同，不同返回2
        if (!u.getPassword().equals(user.getPassword())){
            u.setPassword("");
            return u;
        }

        //校验通过返回登录成功状态码1

        return u;
    }
}
